package thread.demo09;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Run As -> Run Configurations -> Arguments -> VM arguments -Xmx256m
 * 
 * @author mengzhang6
 *
 */
public class ThreadLocalOOMDemo {
	private static final int THREAD_LOOP_SIZE = 500;
	private static final int MOCK_DIB_DATA_LOOP_SIZE = 10000;

	private static ThreadLocal<List<User>> threadLocal = new ThreadLocal<>();

	public static void main(String[] args) throws InterruptedException {

		ExecutorService executorService = Executors.newFixedThreadPool(THREAD_LOOP_SIZE);

		for (int i = 0; i < THREAD_LOOP_SIZE; i++) {
			executorService.execute(() -> {
				threadLocal.set(new ThreadLocalOOMDemo().addBigList());
				Thread t = Thread.currentThread();
				System.out.println(Thread.currentThread().getName());

				// 不取消注释的话就可能出现OOM
				// 避免ThreadLocal在使用线程池时产生OOM
				// threadLocal.remove();
			});
			try {
				Thread.sleep(10L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		// executorService.shutdown();
	}

	private List<User> addBigList() {
		List<User> params = new ArrayList<>(MOCK_DIB_DATA_LOOP_SIZE);
		for (int i = 0; i < MOCK_DIB_DATA_LOOP_SIZE; i++) {
			params.add(new User("xuliugen", "password" + i, "男", i));
		}
		return params;
	}

	class User {
		private String userName;
		private String password;
		private String sex;
		private int age;

		public User(String userName, String password, String sex, int age) {
			this.userName = userName;
			this.password = password;
			this.sex = sex;
			this.age = age;
		}
	}
}
